package com.example.easybuy.controller;


import com.alibaba.fastjson.JSON;
import com.example.easybuy.pojo.EasybuyNews;
import com.example.easybuy.pojo.EasybuyUser;
import com.example.easybuy.service.EasybuyUserService;
import com.example.easybuy.util.ImageUtil;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.net.HttpRequest;
import com.sun.deploy.net.HttpResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 张子晨
 * @since 2021-03-12
 */
@RestController
@RequestMapping("/easybuyUser")
public class EasybuyUserController {

    @Autowired
    EasybuyUserService easybuyUserService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    //查询用户是否存在
    @RequestMapping("searchUser")
    public String searchUser(String loginName, String password) {
        String id = easybuyUserService.searchUser(loginName, password);
        return id;
    }

    //用户登录
    @RequestMapping("dologin")
    public String login(String loginName, String password) {
        EasybuyUser user = easybuyUserService.login(loginName, password);
        Map<String, Object> map = new HashMap<>();
        String token = "";
        if (user != null) {
            //存到redis数据库
            token = UUID.randomUUID() + "";
            redisTemplate.opsForValue().set(token, user);
            Object user1 = redisTemplate.opsForValue().get(token);
            System.out.println("+++++++++++++++++" + user1);
            map.put("token", token);
            map.put("user1", user1);
        } else {
            return null;
        }
        System.out.println("---------------------" + JSON.toJSONString(map));
        return JSON.toJSONString(map);
    }

    //查看token过期时间
    @RequestMapping("getExpire")
    public String getExpire(HttpServletRequest request) {
        String token = request.getHeader("token");
        System.out.println("|||||||||||||||||" + token);
        String exp = "";
//        token=token==null?"":token;
        //查询token在redis中的剩余时间
        Long expire = redisTemplate.getExpire(token);
        System.out.println("~~~~~~~~~~~~~~~~~~" + expire);
        if (expire == -2) {
            exp = "true";
        } else {
            exp = "false";
        }
        return exp;
    }

    //更换验证码图片
    String str = "";

    @RequestMapping("changeCode")
    public String changeCode() {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream("E:\\project\\eSuCai\\images\\imageCode\\aaa.jpg");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        str = ImageUtil.getRundomStr();
        System.out.println(str);
        try {
            ImageUtil.getImage(str, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "image";
    }

    //判断输入的验证码是否相同
    @RequestMapping("sameCode")
    public String sameCode(String code) {
        System.out.println("++++++++++++++++++++" + code);
        System.out.println("--------------------" + str);
        String status = "";
        if (code.equalsIgnoreCase(str)) {
            status = "true";
        }
        return status;
    }

    //用户注册
    @RequestMapping("register")
    public String register(String loginName, String password, String email, String phone) {
        int i = easybuyUserService.registerUser(loginName, password, email, phone);
        String status = "";
        if (i > 0) {
            status = "true";
        } else {
            status = "false";
        }
        return status;
    }

    //查询登录名是否重名
    @RequestMapping("loginNameCount")
    public String loginNameCount(String loginName) {
        int count = easybuyUserService.loginNameCount(loginName);
        String status = "";
        if (count > 0) {
            status = "true";
        } else {
            status = "false";
        }
        return status;
    }

    //注销用户
    @RequestMapping("/logOut")
    public String logOut(HttpServletRequest request) {
        String token = request.getHeader("token");
        redisTemplate.delete(redisTemplate.keys(token));
        return "logOut";
    }

    //分页展示全部用户
    @RequestMapping("/allUser")
    public String allUser(String pageNum, String pageSize) {
        PageInfo<EasybuyUser> pageInfo = easybuyUserService.userAll(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        return JSON.toJSONString(pageInfo);
    }

    //删除用户
    @RequestMapping("/delUser")
    public int delUser(String id){
        return easybuyUserService.delUser(Integer.parseInt(id));
    }
}

